<?php

namespace frontend\controllers;
use common\models\Region;
use common\models\Company;
use common\components\Myhelpers;
use yii;
use yii\bootstrap\Alert;

class MembersController extends FController
{

public $layout="left.php";
public $enableCsrfValidation = false;
public function init(){
        parent::init();
        if(Yii::$app->user->isGuest){
            $this->redirect('/site/login');
            Yii::$app->end();
        }
    }
//会员认证

public function actionCer(){

             //查询省份
            $province = Region::find()
                    ->select('id,name')
                    ->where(['level'=>1])
                    ->asArray()
                    ->orderBy("id asc")
                    ->all();

            //用传过来的省的ID查询下级城市---默认查询一个三级区域
            $province_id = Myhelpers::RemoveXSS(Yii::$app->request->post('province_id'));

        if($province_id){
            $city = Region::find()
                    ->select('id,name')
                    ->where(['level'=>2,'parent_id'=>$province_id])
                    ->asArray()
                    ->all();
            $city[0]['list'] = Region::find()
                    ->select('id,name')
                    ->where(['level'=>3,'parent_id'=>$city[0]['id']])
                    ->asArray()
                    ->all();
            return json_encode($city);
        }

        //用传过来的市的ID查询下级地区
        $city_id = Myhelpers::RemoveXSS(Yii::$app->request->post('city_id'));
            
        if($city_id){
            $town = Region::find()
                    ->select('id,name')
                    ->where(['level'=>3,'parent_id'=>$city_id])
                    ->asArray()
                    ->all();
            return json_encode($town);
        }

            $username = Yii::$app->user->identity->username;
            $cer = company::find()
                    ->where(['username'=>$username])
                    ->orderBy('status asc')
                    ->asArray()
                    ->One();
               
        switch ($cer['status']) {
            case '0':
                //echo "<script> alert('审核中');</script>";
                Yii::$app->view->params['page']['page_title'] = '企业审核 - ' . $this->w_config['website_name'];
                return $this->render('cerer',['cer'=>$cer]);
                break;
            case '1':
               $cer['province'] = region::find()
                    ->select('name')
                    ->where(['id'=>$cer['province']])
                    ->asArray()
                    ->One();
                $cer['city'] = region::find()
                    ->select('name')
                    ->where(['id'=>$cer['city']])
                    ->asArray()
                    ->One();
                Yii::$app->view->params['page']['page_title'] = '企业审核成功 - ' . $this->w_config['website_name'];   
                return $this->render('cersan',['cer'=>$cer]);
                break;
            case '2':
                //echo "<script> alert('审核失败');</script>";
                Yii::$app->view->params['page']['page_title'] = '企业审核失败 - ' . $this->w_config['website_name'];
                return $this->render('cer',['cer'=>$cer,'province'=>$province]);
                break;
            default:
                 $company=Yii::$app->request->post();
            if($company){
                $username = Yii::$app->user->identity->username;
                $time=date("Y-m-d H:i:s",time()+8*60*60);
                $company['username'] = $username;
                $company['create_time'] = $time;
                    $save = Yii::$app->db->createCommand()
                        ->insert('company',$company)
                        ->execute();
                    if($save){
                        echo "<script> alert('提交成功');</script>";
                        return $this->render('cerer',['cer'=>$cer,'province'=>$province]);
                    }else{
                        echo "<script> alert('请检查数据后再次提交');parent.location.href='cer';</script>";
                    }
               }else{
               Yii::$app->view->params['page']['page_title'] = '企业认证 - ' . $this->w_config['website_name'];
                return $this->render('cer', [
                    'province' => $province,
                    'cer' => $cer,
                ]);

    }
    }
       

  

}
//审核失败提交判断
public function actionSbtj(){
            $company=Yii::$app->request->post(); 
            if($company){
            $username = Yii::$app->user->identity->username;
            $time=date("Y-m-d H:i:s",time()+8*60*60);
            $company['username'] = $username;
            $company['create_time'] = $time;
            $save = Yii::$app->db->createCommand()
                ->insert('company',$company)
                ->execute();
            if($save){
                echo "<script> alert('提交成功');parent.location.href='cer';</script>";
             }else{
                echo "<script> alert('请检查数据后再次提交');parent.location.href='cer';</script>";
            }

        }
                
                    

}
//审核中
public function actionCerer(){
           
    return $this->redirect('cer');

}
//审核成功
public function actionCersan(){
  
  return $this->redirect('cer');
       
}
//审核失败
public function actionCersb(){

  return $this->render('cersb');
}

}
